package com.zl.learning.io.test.com.ronsoft.books.nio.channels;

/**
 * Created by zhaolei on 2017/5/2.
 */

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

/**
 * 如果使用的是JDK 1.7，那么Files.walkFileTree会是一个很好的选择。这个类位于java.nio.file包下，这个包是JDK 1.7中新增的
 * @author Administrator
 *
 */
public class FileFinder2 {
    public static void main(String[] args) throws IOException {
        Path startingDir = Paths.get("");
        FindFileVisitor findJavaVisitor = new FindFileVisitor(".txt");
        Files.walkFileTree(startingDir, findJavaVisitor);
        for (String name : findJavaVisitor.getFilenameList()) {
            System.out.println(name);
        }
    }

    static class FindFileVisitor extends SimpleFileVisitor<Path> {
        private List<String> filenameList = new ArrayList<String>();
        private String fileSuffix = null;
        public FindFileVisitor(String fileSuffix) {
            this.fileSuffix = fileSuffix;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            if (file.toString().endsWith(fileSuffix)) {
                filenameList.add(file.toString());
            }
            return FileVisitResult.CONTINUE;
        }

        public List<String> getFilenameList() {
            return filenameList;
        }

        public void setFilenameList(List<String> filenameList) {
            this.filenameList = filenameList;
        }
    }
}


